﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BackgroundBuild : MonoBehaviour
{
    public Sprite background;
    public int count = 10;
    public Vector3 beginPoint;
    public Vector3 offset = Vector3.right;
    public Vector3 scale = Vector3.one;

    private List<GameObject> _backgrounds = new List<GameObject>();

    private void OnGUI()
    {
        if (GUILayout.Button("编译地图"))
        {
            Clear();
            Build();
        }
    }

    public void Build()
    {
        var pos = beginPoint;
        for (int i = 0; i < count; i++)
        {
            var newGameObject = new GameObject("background");
            var spriteRenderer = newGameObject.AddComponent<SpriteRenderer>();
            spriteRenderer.sprite = background;
            spriteRenderer.sortingLayerName = "Background";
            spriteRenderer.sortingOrder = -100;
            newGameObject.transform.position = pos;
            newGameObject.transform.localScale = scale;


            _backgrounds.Add(newGameObject);

            pos += offset;
        }
    }

    public void Clear()
    {
        _backgrounds.ForEach(Destroy);
        _backgrounds.Clear();
    }
}